#include "AnimationSet.h"

using namespace Ogre;
//create textureUnitState, loaded to VRam
void AnimationSet::AnimationSetCreate( const String id,const String nameHead, short frameCount, short frameinterval, short frameStart)
{

	state = INITIALIZING;
	oneLoopFinished = false;

	_nameHead = nameHead;
	_frameCount = frameCount;
	_frameInterval = frameinterval;
	_frameStart = frameStart;


	_pBillBoard = mSceneManager->createBillboardSet("BBSet"+nameHead +id, 1);
	_pBillBoard->setBillboardType(BBT_POINT);
	_pBillBoard->setBillboardOrigin(BBO_TOP_CENTER);
	_pBillBoard->setMaterialName("BOOM/Boom001");

	Billboard* bb = _pBillBoard->createBillboard(Vector3(0, 0, 0));
	FloatRect coords(0.0, 0.0, 1.0, 1.0);
	bb->setTexcoordRect(coords);
	bb->setDimensions(Real(100), Real(200));
	



	_mNode = mSceneManager->getRootSceneNode()->createChildSceneNode("BillBoard"+nameHead +id);
	_mNode->attachObject(_pBillBoard);
	_pBillBoard->setRenderQueueGroup(RENDER_QUEUE_MAIN + 1);
	_mNode->setPosition(Vector3(0,10,10));


	//load all texture set to VRam in advance
	MaterialPtr material = MaterialManager::getSingleton().getByName("BOOM/Boom001");
	for(int i = _frameStart; i< _frameStart + _frameInterval * (_frameCount - 1); i+= _frameInterval)
	{

		char *buffer = (char*)malloc(sizeof(char) * 10);
		itoa(i, buffer, 10);
		if(i<10)
			//material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(_nameHead + "000" + buffer + ".png");
			TextureManager::getSingleton().load(_nameHead + "0" + buffer + ".png", "Popular");
		else if(i<100)
			//material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(_nameHead + "00" + buffer + ".png");
			TextureManager::getSingleton().load(_nameHead + "" + buffer + ".png", "Popular");
		free(buffer);
	}



	state = INITIALIZED;

}

void AnimationSet::setPosition(Vector3 _p)
{
	_mNode->setPosition(_p);
}

void AnimationSet::setDimension(Vector2 _p)
{
	_pBillBoard->getBillboard(0)->setDimensions(_p.x, _p.y);
}

void AnimationSet::setRenderQueueGroup(Ogre::RenderQueueGroupID id)
{
	_pBillBoard->setRenderQueueGroup(id);
}

void checkIfLoaded()
{

}

//loaded while loop function to play a animationSet
void AnimationSet::play(Real timeSinceLast)
{
	if(state = INITIALIZED)
	{
		oneLoopFinished = false;
		
		timeCurrent += timeSinceLast;
		if(lastTime == 0)
			lastTime = timeCurrent;

		MaterialPtr material = MaterialManager::getSingleton().getByName("BOOM/Boom001");
		char *buffer = (char*)malloc(sizeof(char) * 10);;
		itoa(_currentFrame, buffer, 10);
		if( _currentFrame< 10 )
			material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(_nameHead + "0" + buffer + ".png");
		else if( _currentFrame < 100)
			material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(_nameHead + "" + buffer + ".png");

		if((timeCurrent - lastTime)> timeInterval)
		{
			_currentFrame +=_frameInterval;
			lastTime = timeCurrent;
		}

		if(_currentFrame == _frameStart + _frameInterval * (_frameCount - 1) )
		{
			_currentFrame = _frameStart;
			oneLoopFinished = true;
		}

		free(buffer);

	}
}

Ogre::Pass * AnimationSet::getMaterialPass()
{
	return _pBillBoard->getMaterial()->getTechnique(0)->getPass(0);
}

//cease animation
void AnimationSet::cease(){}


//restart, play from the very begining
void AnimationSet::restart(){}

//unload animation set
void AnimationSet::unLoad(){}


void AnimationSet::setTimeInterval(Ogre::Real _t)
{
	timeInterval = _t;
}

void AnimationSet::setVisable(bool _f)
{
	_pBillBoard->setVisible(_f);
}

bool AnimationSet::reportOneRoundFinished()
{
	return oneLoopFinished;
}
void AnimationSet::reset()
{
	oneLoopFinished =false;
}